using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using Yoozoo.UI;
using Yoozoo.UI.YGUI;

public class InputFieldBinder : BinderBase
{
    public enum AttributeType : int
    {
        shouldHideMobileInput,
        text,
        caretBlinkRate,
        caretWidth,
        caretColor,
        customCaretColor,
        selectionColor,
        onEndEdit,
        onValueChanged,
        characterLimit,
        readOnly,
        asteriskChar,
        caretPosition,
        selectionAnchorPosition,
        selectionFocusPosition,
        interactable,
        enabled,
    }
    
    private InputField _target;
    
    public InputFieldBinder(UIExpansion owner, LinkerConfig config) : base(owner, config)
    {
    }

    public InputFieldBinder()
    {
        
    }

    public override void Dispose()
    {
        _target = null;
        base.Dispose();
    }

    public override void Init(LinkerConfig config)
    {
        GameObject targetGo = _owner.GetStoredGameObject(config.StoredGameObjectIndex);
        if (targetGo == null)
        {
            return;
        }
        _target = targetGo.GetComponent<InputField>();
        _active = _target != null;
    }
    public override void SetBoolean(bool value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.shouldHideMobileInput:
                _target.shouldHideMobileInput = value;
            break;
            case AttributeType.customCaretColor:
                _target.customCaretColor = value;
            break;
            case AttributeType.readOnly:
                _target.readOnly = value;
            break;
            case AttributeType.interactable:
                _target.interactable = value;
            break;
            case AttributeType.enabled:
                _target.enabled = value;
            break;
            default:
            break;
        }
        base.SetBoolean(value);
    }
    
    public override void SetString(string value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.text:
                _target.text = value;
            break;
            default:
            break;
        }
        base.SetString(value);
    }
    
    public override void SetSingle(float value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.caretBlinkRate:
                _target.caretBlinkRate = value;
            break;
            default:
            break;
        }
        base.SetSingle(value);
    }
    
    public override void SetInt32(int value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.caretWidth:
                _target.caretWidth = value;
            break;
            case AttributeType.characterLimit:
                _target.characterLimit = value;
            break;
            case AttributeType.caretPosition:
                _target.caretPosition = value;
            break;
            case AttributeType.selectionAnchorPosition:
                _target.selectionAnchorPosition = value;
            break;
            case AttributeType.selectionFocusPosition:
                _target.selectionFocusPosition = value;
            break;
            default:
            break;
        }
        base.SetInt32(value);
    }
    
    public override void SetColor(Color value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.caretColor:
                _target.caretColor = value;
            break;
            case AttributeType.selectionColor:
                _target.selectionColor = value;
            break;
            default:
            break;
        }
        base.SetColor(value);
    }
    
    public override void SetActionString(UnityAction<string> action)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onEndEdit:
                _target.onEndEdit.AddListener(action);
            break;
            case AttributeType.onValueChanged:
                _target.onValueChanged.AddListener(action);
            break;
            default:
            break;
        }
        base.SetActionString(action);
    }
    
    public override void RemoveActionString(UnityAction<string> action)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onEndEdit:
                _target.onEndEdit.RemoveListener(action);
            break;
            case AttributeType.onValueChanged:
                _target.onValueChanged.RemoveListener(action);
            break;
            default:
            break;
        }
        base.RemoveActionString(action);
    }
    
    public override void SetChar(char value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.asteriskChar:
                _target.asteriskChar = value;
            break;
            default:
            break;
        }
        base.SetChar(value);
    }
    
    public override void RemoveAllAction()
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onEndEdit:
                _target.onEndEdit.RemoveAllListeners();
            break;
            case AttributeType.onValueChanged:
                _target.onValueChanged.RemoveAllListeners();
            break;
            default:
            break;
        }
    }
    
}